package com.yyge.aspect;

import com.yyge.annotation.AdminAcces;
import com.yyge.dto.ResultCode;
import com.yyge.exception.ApplicationException;
import com.yyge.util.TokenUtil;
import com.yyge.vo.LoginVO;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AdminAspect {

    @Autowired
    private TokenUtil tokenUtil;

    @Before("@annotation(acces)")
    public void checkAdmin(AdminAcces acces){
        LoginVO vo=tokenUtil.loginVOThreadLocal.get();
        if(vo == null /*令牌过期*/ ){
            throw new ApplicationException(ResultCode.SYSTEM_TKOKEN_EXPIRES);
        }

        if(vo.getAccountType() != vo.ACCOUNT_TYPE_ADMIN /*判断是否是管理员*/ ){
            throw new ApplicationException(ResultCode.SYSTEM_NEED_TKOKEN);
        }

        /*限制特定的访问*/
        String value= acces.value();
        System.out.println(vo.getName());
        System.out.println(value);

        if(!value.isEmpty()/*value不为空*/ && !value.equalsIgnoreCase(vo.getName())){
            throw new ApplicationException(ResultCode.SYSTEM_ACCOUNT_INVALD,"只有管理员 "+value+" 才能访问");
        }
    }

}
